package com.wlr.blog.util.common.dto;

import com.wlr.blog.util.common.constants.ApiStatus;

/**
 * @author lrwang
 * @version 1.0
 * @date 2021/7/19 17:37
 */
public class Result<T> {
    private String code;
    private String message;
    private T result;

    public static Result error(String message) {
        Result result = new Result();
        result.setCode(Integer.toString(ApiStatus.FAIL.getCode()));
        result.setMessage(message);
        return result;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String msg) {
        this.message = msg;
    }

    public T getResult() {
        return result;
    }

    public void setResult(T result) {
        this.result = result;
    }

    public Result() {
    }

    public Result(T data) {
        this.result = data;
    }

    public static Result success() {
        Result result = new Result<>();
        result.setCode("200");
        result.setMessage("操作成功");
        return result;
    }

    public static Result success(String msg) {
        Result result = new Result<>();
        result.setCode("200");
        result.setMessage(msg);
        return result;
    }

    public static <T> Result<T> success(String msg, T data) {
        Result<T> result = new Result<>();
        result.setCode("200");
        result.setMessage(msg);
        result.setResult(data);
        return result;
    }

    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<>(data);
        result.setCode("200");
        result.setMessage("成功");
        return result;
    }

    public static Result error(String code, String msg) {
        Result result = new Result();
        result.setCode(code);
        result.setMessage(msg);
        return result;
    }

    public static <T> Result<T> success(String code, String msg, T data) {
        Result<T> result = new Result<>(data);
        result.setCode(code);
        result.setMessage(msg);
        return result;
    }
}
